// pages/work-detail/work-detail.js
var request = require('../../utils/request.js')
var modal = require('../../utils/modal.js')
var config = require('../../config.js');
let hyMath = require('../../utils/math.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    member_id:'',
    is_show_frozen:0,
    is_show_pay:0,
    is_enable_balance:1,
    member_balance:0, 
    add_bonus: '',
    work_sheet_type: '',//类别
    id: '',
    typePage: 0,
    isShow: false,
    pay_order_id: '',
    create_time: '',
    create_name: '',
    channel_type_name: '',
    license_number: '',
    real_name: '',
    mobile: '',
    level: '',
    total_amount: '',
    work_cost: '',
    parts_cost: '',
    additional_cost: '',
    total_discount: '',
    amount: '',
    invoice_number: '',
    invoice_amount: '',
    is_upload_invoice: '',
    money: 200,//余额
    channel_type_list: {},
    payItemMethod: {
      methodIdx: '',//其他方式id
      card: false,//储值卡
      method: false,//选择了其他
    },
    reqPayParams: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.data.id = options.id

    if (options.type == 0) {
      wx.setNavigationBarTitle({
        title: '工单详情'
      })
      modal.loading()
      request.post({ path: config.workDetail }, { _id: options.id }).then(res => {
        modal.hide()
        if (res.data.data.channel_type_list){
          var temp = res.data.data.channel_type_list
          for (var item in temp){
            temp[item].checked = false
          }
        }

        

        
        
        that.setData({
          pay_order_id: res.data.data.pay_order_id,
          create_time: res.data.data.create_time,
          create_name: res.data.data.create_name,
          channel_type_name: res.data.data.channel_type_name,
          license_number: res.data.data.license_number,
          real_name: res.data.data.real_name,
          mobile: res.data.data.mobile,
          level: res.data.data.level,
          total_amount: res.data.data.total_amount,
          work_cost: res.data.data.work_cost,
          parts_cost: res.data.data.parts_cost,
          additional_cost: res.data.data.additional_cost,
          total_discount: res.data.data.total_discount,
          amount: String(res.data.data.amount).indexOf(".") == -1 ? res.data.data.amount + '.00' : res.data.data.amount,
          typePage: options.type,
          work_sheet_type: res.data.data.work_sheet_type,
          add_bonus: res.data.data.add_bonus,    
          is_show_frozen: res.data.data.is_show_frozen,
          is_show_pay: res.data.data.is_show_pay,
          is_enable_balance: res.data.data.is_enable_balance,
          member_balance: hyMath.hyTofloat2(res.data.data.member_balance)-0,
          channel_type_list: temp,
          member_id: res.data.data.member_id,
        })
      
      })
     
    } else {
      wx.setNavigationBarTitle({
        title: '订单详情'
      })
      modal.loading()
      request.post({ path: config.orderDetail }, { _id: options.id }).then(res => {
        modal.hide()
        that.setData({
          pay_order_id: res.data.data.pay_order_id,
          create_time: res.data.data.create_time,
          create_name: res.data.data.create_name,
          channel_type_name: res.data.data.channel_type_name,
          license_number: res.data.data.license_number,
          real_name: res.data.data.real_name,
          mobile: res.data.data.mobile,
          level: res.data.data.level,
          total_amount: res.data.data.total_amount,
          work_cost: res.data.data.work_cost,
          parts_cost: res.data.data.parts_cost,
          additional_cost: res.data.data.additional_cost,
          total_discount: res.data.data.total_discount,
          amount: res.data.data.amount,
          invoice_number: res.data.data.invoice_number,
          invoice_amount: res.data.data.invoice_amount,
          is_upload_invoice: res.data.data.is_upload_invoice,
          typePage: options.type,
          work_sheet_type: res.data.data.work_sheet_type,
        })
      })


    }

  },
  selectCart() {
    var temp = ''
    if (this.data.payItemMethod.card) {
      temp = false
    } else {
      temp = true
    }
    this.setData({
      "payItemMethod.card": temp
    })
  },
  selectItem(e) {
    var method = ''
    var idx = e.currentTarget.dataset.index 
    var temp = this.data.channel_type_list
    if (this.data.payItemMethod.methodIdx == idx) {
      for(var item in temp){
        if (temp[item].value == idx && temp[item].checked){
          temp[item].checked = false
          idx = ''
          method = false
        }
      }
    } else {
      method = true
    
      for (item in temp){
        if (idx == temp[item].value) {
          temp[item].checked = true

        } else {
          temp[item].checked = false
        }
      }
    }
    this.setData({
      channel_type_list: temp,
      "payItemMethod.methodIdx": idx,
      "payItemMethod.method": method
    })


  },
  callPay() {
    var that = this
    if (this.data.payItemMethod.methodIdx === 1) {
      wx.scanCode({
        success: function (res) {
          that.scanTap(res)
        },
        fail: function (res) {
          wx.showToast({
            icon: 'none',
            title: '支付失败'
          })
        }
      })
    } else {
      that.scanTap()
    }

  },
  scanTap(res) {
    var that = this;
    var is_use_balance = ''
    var channel_type = ''
    if (that.data.payItemMethod.card) {
      is_use_balance = 1
    } else {
      is_use_balance = 0
    }
   
    if (that.data.payItemMethod.method) {
      channel_type = that.data.payItemMethod.methodIdx
    } else {
      channel_type = 0
    }


    var data = {

      "_id": String(that.data.id), //工单唯一_id
      "is_use_balance": that.data.is_enable_balance == 0 ? 0 : is_use_balance , // 是否使用储值 1标识是 0，表示否  //如果是储值工单传0，不是的话可选储值也可不选
      "channel_type": channel_type, //支付渠道, 没有选就是0. 渠道：1=微信、2=支付宝、3=POS、4=现金、5=汇款、6=支票、7=储值、8=应收、9=定金、10=综合券

    }
    if (res) {
      data.code = res.result       //渠道是微信的时候，扫描微信付款二维码获得的支付码
    }

    //支付
    request.post({ 'path': config.orderPay }, data).then(function (resData) {
      var ratingData = resData.data;
      if (ratingData.ret == 0) {
        if (ratingData.data.pay_status == 2) {
          wx.navigateTo({
            url: `../Qr-code-collection/Qr-code-collection?orderId=${that.data.id}`
          })
        } else if (ratingData.data.pay_status == 3) {
          wx.navigateTo({
            url: `../pay-success/pay-success?orderId=${that.data.id}`
          })
        } else if (ratingData.data.pay_status == 4) {
          wx.navigateTo({
            url: `../pay-error/pay-error?orderId=${that.data.id}`
          })
        }

      } else {
        wx.showToast({
          icon: 'none',
          title: ratingData.msg
        })
        return false;
      }
    }, function (resData) {
      wx.showToast({
        icon: 'none',
        title: resData.data.msg
      })
      return false;
    })

  },

  //工单冻结
  frozen() {
    var that = this
    request.post({ path: config.workFrozen }, { _id: this.data.id }).then(res => {
      if (res.data.data.result) {
        modal.toast('冻结成功')
        setTimeout(function () {
          wx.navigateTo({
            url: `../work-center/work-center?type=1`,
          })
        }, 1000)
      }

    })
  },
  toHome() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  callpay() {
    this.setData({
      isShow: true
    })
  },
  close() {
    this.setData({
      isShow: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})